<?php

namespace App\Http\Controllers\Admin\Framework\Manage;

use App\Http\Controllers\Admin\ParentController;
use App\Master\Framework\Extend\DataUtil;
use App\Master\Framework\View\AdminData;
use App\Models\Admin\Framework\AdminMenusModel;
use App\Models\Admin\Framework\AdminRoleModel;
use App\Utils\AppResult;
use Illuminate\Http\Request;

class MenusController extends ParentController
{

    public function __construct(array $attributes = [])
    {
        parent::__construct();
    }

    public function index(Request $request)
    {
        if ($request->isMethod('post')) {
            $menus = AdminMenusModel::getMenuRecursion([1, 2]);
            return AppResult::response200('success', $menus);
        } else {
            return view('admin.framework.manage.menus.index');
        }
    }

    public function insert(Request $request)
    {
        $params = $request->only([
            'parent_id', 'name', 'path', 'action', 'icon', 'status', 'sort', 'facility'
        ]);

        if (!AdminMenusModel::insertMenu($params, $request->cud ?? [])) {
            return AppResult::response201('菜单添加失败，请稍后再试！');
        }
        return AppResult::response200('菜单添加成功！');
    }

    public function update(Request $request)
    {
        $params = $request->only([
            'parent_id', 'name', 'path', 'action', 'icon', 'status', 'sort', 'facility'
        ]);
        AdminMenusModel::updateMenu($params, $request->id);
        return AppResult::response200('操作成功');
    }

    public function status(Request $request)
    {
        $params = $request->all();
        if (empty($params['id']) || !isset($params['status'])) {
            return AppResult::response203('操作超时！');
        }

        if ($params['status'] == 1) {
            if (AdminMenusModel::query()->where('parent_id', $params['id'])->whereIn('status', [1, 2])->where('is_delete', 0)->exists()) {
                return AppResult::response201('当前数据存在子集，请先禁用子集！');
            }
        }

        $form['status']     = $params['status'] == 1 ? 2 : 1;
        $form['updated_at'] = time();
        if (!AdminMenusModel::query()->where('id', $params['id'])->update($form)) {
            return AppResult::response201('操作失败');
        }
        return AppResult::response200('操作成功');
    }

    public function remove(Request $request)
    {
        $params = $request->all();
        if (empty($params['id'])) {
            return AppResult::response203('操作超时！');
        }

        if (AdminMenusModel::query()->where('parent_id', $params['id'])->whereIn('status', [1, 2])->where('is_delete', 0)->exists()) {
            return AppResult::response201('当前数据存在子集，请先删除子集！');
        }

        $form['is_delete']  = 1;
        $form['updated_at'] = time();
        if (!AdminMenusModel::query()->where('id', $params['id'])->update($form)) {
            return AppResult::response201('操作失败');
        }
        return AppResult::response200('操作成功');
    }

    /**
     * 排序
     * @return string
     */
    public function rank(Request $request)
    {
        $params = $request->all();
        if (!isset($params['id']) || !isset($params['sort'])) {
            return AppResult::response203('操作超时！');
        }

        $form['sort']       = $params['sort'];
        $form['updated_at'] = time();

        if (!AdminMenusModel::query()->where('id', $params['id'])->update($form)) {
            return AppResult::response201('操作失败');
        }

        return AppResult::response200('操作成功');
    }
}